package Operate;

import Manu.Manu;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Login{
    public static void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入您的密码:");
        String password = scanner.nextLine();

        if (Check(username, password)) {
            System.out.println("登录成功！");
            Manu.manu();
        } else {
            System.out.println("登录失败，请检查用户名和密码，重新输入！");
            login();
        }
    }

    private static boolean Check(String username, String password) {
        try {
            File file = new File("UserInformation");
            Scanner fileScanner = new Scanner(file);
            while (fileScanner.hasNextLine()) {
                String line = fileScanner.nextLine();
                String[] input = line.split(",");
                if (input.length == 2 && input[0].equals(username) && input[1].equals(password)) {
                    fileScanner.close();
                    return true;
                }
            }
            fileScanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}
